let maxPage = 0  //最大页面数
let nowPage = 1  //当前页 
let OrderNumber = 0  //要删除的订单号
let nowloginUser = getData('loginUser') //取登录者信息
let nowAccessLevel = nowloginUser.AccessLevel //取登录者权限等级
 //let nowloginUser = { id: 14, jobNumber: 10014, password: '12345', userName: '李四', sex: '女', Birthdate: '1990/12/12', tel: '133xxxxx', headPortrait: '../images/后勤.webp', AccessLevel: 1, jobTitle: '后勤' }
 let newData = {} //新增的预定数据
//获取数据
let roomData = getData('room')
let reserveData = getData('reserveOrder')
let OrderCountData = getData('OrderCount')
//待入住的房间
let roomNumArr = []
// for(let i = 0;i<roomData.length;i++){
//     if(roomData[i].roomState == '待入住'){
//         roomNumArr.push(roomData[i].roomNumber)
//     }
// }
// function getroom(){
//     for(let i = 0;i<roomData.length;i++){
//         if(roomData[i].roomState == '待入住'){
//             nullroom = roomData[i].roomNumber
//         }
// }
//渲染分页
function showPage() {
    let pageNum = Math.ceil(reserveData.length / 10)
    maxPage = pageNum
    $('.PageBtn').remove()
    for (let i = 1; i <= pageNum; i++) {
        let liStr = `<li class='PageBtn'><a src='#'>${i}</a></li>`
        $('#next').before(liStr)
    }
    changeLi(0)
}
showPage()
//给分页按钮绑定点击事件
$('.pagination').on('click', '.PageBtn', function () {
    let pageNum = parseInt($(this).text())
    changeLi(pageNum-1)
    nowPage = pageNum
    showData(pageNum)
})
//分页按钮变色
function changeLi(a) {
    for (let i = 0; i < $('.PageBtn').length; i++) {
         $('.PageBtn').eq(i).removeClass('active')
    }
    $('.PageBtn').eq(a).addClass('active')
}
//渲染数据
function showData(pageNum) {
    let start = (pageNum - 1) * 10
    let end = 10 * pageNum - 1
    if (pageNum == maxPage) {
        end = reserveData.length - 1
    }
    $('.tBody').html('')
    for (let i = start; i <= end; i++) {
        let str =
            `
            <tr>
                <td>${reserveData[i].orderNumber}</td>
                <td>${reserveData[i].roomNumber}</td>
                <td>${reserveData[i].reserveTimr}</td>
                <td>${reserveData[i].checkInTime}</td>
                <td>${reserveData[i].leaveTime}</td>
                <td>${reserveData[i].price}</td>
                <td>${reserveData[i].cashPledge}</td>
                <td>${reserveData[i].clientName}</td>
                <td>${reserveData[i].tel}</td>
                <td>${reserveData[i].orderState}</td>
                <td>${reserveData[i].inputter}</td>
                <td>
                    <span class="iconfont icon-xiugai mod" data-toggle="modal" data-target="#checkInOperation">
                    </span>
                    <span class='iconfont del icon-ziyuanxhdpi' data-toggle="modal" data-target="#delhint"></span>
                </td>
            </tr>
            `
        $('.tBody').append(str)
    }
}
showData(1)

//点击上一页按钮
$('#pre').click(function () {
    nowPage--
    if (nowPage == 0) {
        showData(1)
        changeLi(nowPage-1)
        nowPage = 1
    }
    else {
        showData(nowPage)
        changeLi(nowPage-1)
    }
})
//点击下一页按钮
$('#next').click(function () {
    nowPage++
    if (nowPage == maxPage + 1) {
        showData(maxPage)
        changeLi(nowPage-1)
        nowPage = maxPage
    }
    else {
        console.log(nowPage);
        changeLi(nowPage-1)
        showData(nowPage)
    }
})

//删除按钮绑定事件
$('.tBody').on('click', '.del', function(){
    if(nowAccessLevel<2){
        let delOrderNum = $(this).parents('tr').children().eq(0).text()
        OrderNumber = delOrderNum
    }
    else{
        alert('权限不够，无法删除')
        $(this).removeAttr('data-target');
    }  
})
//删除数据
$('#delhint').on('click','#confirm', function(){
    for (let i = 0; i < reserveData.length; i++) {
        if (reserveData[i].orderNumber == OrderNumber) {
            reserveData.splice(i, 1)
            delData('reserveOrder')
            saveData('reserveOrder', reserveData);
            showData(nowPage)
            showPage()    
        }
    }
})
//点击修改
$('.tBody').on('click','.mod', function() {
    if(nowAccessLevel<2){
        let orderNum = $(this).parents('tr').children('td').eq(0).text()
        let roomNum = $(this).parents('tr').children('td').eq(1).text()
        let reserveTime = $(this).parents('tr').children('td').eq(2).text()
        let checkinTime = $(this).parents('tr').children('td').eq(3).text()
        let leaveTime = $(this).parents('tr').children('td').eq(4).text()
        let price = $(this).parents('tr').children('td').eq(5).text()
        let cash = $(this).parents('tr').children('td').eq(6).text()
        let person = $(this).parents('tr').children('td').eq(7).text()
        let tel = $(this).parents('tr').children('td').eq(8).text()
        let orderStatus = $(this).parents('tr').children('td').eq(9).text()
        
        $('#orderNumMod').val(orderNum)
        $('#roomNumMod').val(roomNum)
        $('#reserveTimeMod').val(reserveTime)
        $('#checkinTimeMod').val(checkinTime)
        $('#leaveTimeMod').val(leaveTime)
        $('#priceMod').val(price)
        $('#cashMod').val(cash)
        $('#personMod').val(person)
        $('#telMod').val(tel)
        $('#orderStatusMod').val(orderStatus)
    }
    else{
        alert('权限不够，无法修改')
        $(this).removeAttr('data-target');
    }
})

//确认修改
$('#checkInOperation #confirm').click(function () {
    let orderNum = $('#orderNumMod').val()
    let roomNum = $('#roomNumMod').val()
    // let reserveTime =  $('#reserveTime').val()
    let checkinTime = $('#checkinTimeMod').val()
    let leaveTime = $('#leaveTimeMod').val()
    let price = $('#priceMod').val()
    let cash = $('#cashMod').val()
    let person = $('#personMod').val()
    let tel = $('#telMod').val()
    let orderStatus = $('#orderStatusMod option:selected').val()
    for (let i = 0; i < reserveData.length - 1; i++) {
        if (orderNum == reserveData[i].orderNumber) {
            reserveData[i].roomNumber = roomNum
            reserveData[i].checkInTime = checkinTime
            reserveData[i].leaveTime = leaveTime
            reserveData[i].price = price
            reserveData[i].cashPledge = cash
            reserveData[i].clientName = person
            reserveData[i].tel = tel
            reserveData[i].orderState = orderStatus
            reserveData[i].inputter = nowloginUser.userName
        }
    }
    delData('reserveOrder')
    saveData('reserveOrder', reserveData)
    showData(nowPage)
    showPage() 
})
//增加数据

$('.addInfo').click(function(){
    if(nowAccessLevel ==2){
        alert('权限不够，无法增加')
        $(this).removeAttr('data-target');
    }
    else{
            $('.hint').remove()
            roomNumArr = []
            // 清空输入框的数据
            $('#orderNum').val('')
            $('#roomNum').val('')
            $('#checkinTime').val('')
            $('#leaveTime').val('')
            $('#price').val('')
            $('#cash').val('')
            $('#person').val('')
            $('#tel').val('')   
            $('#orderStatus').val('')
            $('#operater').val('')

            //获取订单号
            OrderCountData++
            let OrderCountStr = String(OrderCountData)
            let attr = OrderCountStr.split('')
            $('#cash').val('100')
            $('#orderStatus').val('预定中')
            $('#operater').val(nowloginUser.userName)
            let orderNum
            if(attr.length == 1){
                orderNum = getDate()+'00'+OrderCountStr
            }
            else if(attr.length == 2){
                orderNum = getDate()+'0'+OrderCountStr
            }
            else if(attr.length == 3){
                orderNum = getDate()+OrderCountStr
            }
            $('#orderNum').val(orderNum)
            //获取房间号对应的价格
            $('#roomNum').blur(function(){
                let count = 0 //状态值
                $('.hint').remove()
                let roomNum = $('#roomNum').val()
                for(let i = 0;i<roomData.length;i++){
                    if(roomData[i].roomState == '待入住'){
                        let nullRoom = {
                            roomNum :roomData[i].roomNumber,
                            price:roomData[i].price
                        }
                        roomNumArr.push(nullRoom)
                    }
                }
                for(let i = 0 ;i<roomNumArr.length;i++){
                    if(roomNum == roomNumArr[i].roomNum){
                         $('#price').val(roomNumArr[i].price)
                         break
                    }
                    count++
                }
                if(roomNum !='' && count == roomNumArr.length){
                    $('#price').val('')
                    let htmlStr = `<span class='hint'>此房间不可用</span>`
                    $('#roomNum').after(htmlStr)
                }
            })
    }
})
// 取消增加数据
$('#addReserve #cancel').click(function(){
        // 清空输入框的数据
        $('#orderNum').val('')
        $('#roomNum').val('')
        $('#checkinTime').val('')
        $('#leaveTime').val('')
        $('#price').val('')
        $('#cash').val('')
        $('#person').val('')
        $('#tel').val('')   
        $('#orderStatus').val('')
        $('#operater').val('')
})
//确认增加数据
$('#addReserve #confirm').click(function(){
    //获取id号
    let lastId = reserveData[reserveData.length-1].id
    let newIdNum = parseInt(lastId.slice(1,3))+1 
    let newId = 'd'+ newIdNum
    //获取订单号Data
    OrderCountData++
    let OrderCountStr = String(OrderCountData)
    let attr = OrderCountStr.split('')
    let orderNum
    if(attr.length == 1){
        orderNum = getDate()+'00'+OrderCountStr
    }
    else if(attr.length == 2){
        orderNum = getDate()+'0'+OrderCountStr
    }
    else if(attr.length == 3){
        orderNum = getDate()+OrderCountStr
    }
    let roomNum = $('#roomNum').val()
    let price = $('#price').val()
    let reserveTime =  getTime()
    let checkinTime = $('#checkinTime').val()
    let leaveTime = $('#leaveTime').val()
    let person = $('#person').val()
    let tel = $('#tel').val()
    newData = {
        id:newId,
        orderNumber:orderNum,
        roomNumber:roomNum,
        reserveTimr:reserveTime,
        checkInTime:checkinTime,
        leaveTime:leaveTime,
        price:price,
        cashPledge:'100',
        clientName:person,
        tel:tel,
        orderState:'预定中',
        inputter:nowloginUser.userName
    }
    for(let i = 0;i<roomData.length;i++){
        if(roomNum == roomData[i].roomNumber){
            roomData[i].roomState = '已入住'
        }
    }
    delData('room')
    saveData('room',roomData)
    reserveData.unshift(newData)
    delData('reserveOrder')
    saveData('reserveOrder', reserveData)
    showData(nowPage)
    showPage() 
    newData = {}
})
//查询员工信息
//1.获取搜索框里面的值
//2.根据搜索框里面的值与数据库进行比较
//3.把符合的值显示在表格里
let searchType = 'option1'
let searchArr= []
let searchNowPage = 1
let searchMaxPage
//根据不同选项类型，显示不同提示词
$('#typeOption').click(function(){
    $('#OptionType').val('')
     searchType = $(this).val()
     if($(this).val() =='option1'){
          $('#OptionType').attr('placeholder','请输入订单号')
     }
     else if($(this).val() =='option2'){
          $('#OptionType').attr('placeholder','请输入房间号')
     }
     else{
          $('#OptionType').attr('placeholder','请输入客户名')
     }
})

$('#search').click(function(event){
    event.preventDefault()
    let str = `
    <li id="pre1">
        <a href="#" aria-label="Previous">
            <span aria-hidden="true">&laquo;</span>
        </a>
    </li>
    <li id="next1">
        <a href="#" aria-label="Next">
            <span aria-hidden="true">&raquo;</span>
        </a>
    </li>
    `
    $('.pagination').show()
    searchArr.splice(0,searchArr.length) //每次搜索都清空数组
    $('.tBody').html('')
    $('.pagination li').remove()
    $('.pagination').append(str)
    if($('#typeOption').val() == 'option1'){
        let order = $('#OptionType').val()
        for(let i = 0;i<reserveData.length;i++){
            if(order == reserveData[i].orderNumber){
                 searchArr.push(reserveData[i])
            }
        }
        if(searchArr.length == 0){
            $('.pagination').hide()
            alert('没有此订单号')
        }
        else{
            showSerchPage()
            showSerchData()
        }    
    }
    else if($('#typeOption').val() == 'option2'){
        let order = $('#OptionType').val()
        for(let i = 0;i<reserveData.length;i++){
            if(order == reserveData[i].roomNumber){
                 searchArr.push(reserveData[i])
            }
        }
        if(searchArr.length == 0){
            $('.pagination').hide()
            alert('没有此房间号')
        }
        else{
            showSerchPage()
            showSerchData()
        }
    }
    else if($('#typeOption').val() == 'option3'){
        let order = $('#OptionType').val()
        for(let i = 0;i<reserveData.length;i++){
            if(order == reserveData[i].clientName){
                 searchArr.push(reserveData[i])
            }
        }
        if(searchArr.length == 0){
            $('.pagination').hide()
            alert('没有此客户')
        }
        else{
            console.log(searchArr)
            showSerchPage()
            showSerchData()
        }
    }
})
//渲染搜索出来的数据
function showSerchData(){
    $('.tBody').html('')
    let start = (searchNowPage - 1) * 10
    let end = 10 * searchNowPage - 1
    if (searchNowPage == searchMaxPage) {
        end = searchArr.length - 1
    }
    for (let i = start;i <=end; i++) {
        let str =
            `
            <tr>
                <td>${searchArr[i].orderNumber}</td>
                <td>${searchArr[i].roomNumber}</td>
                <td>${searchArr[i].reserveTimr}</td>
                <td>${searchArr[i].checkInTime}</td>
                <td>${searchArr[i].leaveTime}</td>
                <td>${searchArr[i].price}</td>
                <td>${searchArr[i].cashPledge}</td>
                <td>${searchArr[i].clientName}</td>
                <td>${searchArr[i].tel}</td>
                <td>${searchArr[i].orderState}</td>
                <td>${searchArr[i].inputter}</td>
                <td>
                    <span class="iconfont icon-xiugai mod" data-toggle="modal" data-target="#checkInOperation">
                    </span>
                    <span class='iconfont del icon-ziyuanxhdpi' data-toggle="modal" data-target="#delhint"></span>
                </td>
            </tr>
            `
        $('.tBody').append(str)
    }
}
//渲染搜索分页
function showSerchPage(){
    let searchNowPage = Math.ceil(searchArr.length / 10)
    searchMaxPage = searchNowPage
    $('.serchPageBtn').remove()
    for (let i = 1; i <= searchNowPage; i++) {
        let liStr = `<li class='serchPageBtn'><a src='#'>${i}</a></li>`
        $('#next1').before(liStr)
    }
    changeLiSerch(0)
}

$('.pagination').on('click','.serchPageBtn', function(){
    let pageNum = parseInt($(this).text())
    changeLiSerch(pageNum-1)
    searchNowPage = pageNum
    showSerchData()
})

//分页按钮变色
function changeLiSerch(a) {
    for (let i = 0; i < $('.serchPageBtn').length; i++) {
         $('.serchPageBtn').eq(i).removeClass('active')
    }
    $('.serchPageBtn').eq(a).addClass('active')
}
//点击上一页
$('.pagination').on('click','#pre1',function(){
    searchNowPage--
    if (searchNowPage == 0) {
        searchNowPage = 1
        showSerchData()
        changeLiSerch(searchNowPage-1)
    }
    else {
        showSerchData()
        changeLiSerch(searchNowPage-1)
    }
})
//点击下一页按钮
$('.pagination').on('click','#next1',function(){
    searchNowPage++
    if (searchNowPage == searchMaxPage + 1){
        searchNowPage = searchMaxPage
        showSerchData()
        changeLiSerch(searchNowPage-1)
    }
    else {
        showSerchData()
        changeLiSerch(searchNowPage-1)
    }
})


// 显示当前登录者头像及工号
showHeadImgId()
      
// 头部退出按钮
$('#quit').find('button').click(function(){
    open('./login.html','_self')
}) 